#include <dde_kit/dde_kit.h>
#include <dde_kit/pgtab.h>
#include <dde_kit/timer.h>
#include <dde_kit/thread.h>
#include <dde_kit/types.h>

#include <eku/stdio.h>
#include <eku/stdlib.h>
#include <eku/sync.h>
#include <inc/x86.h>
#include <inc/x86_lock.h>

#include "conf.h"

extern dde_kit_addr_t *inv_map;
extern spin_lock_t dk_timer_lock;
extern spin_lock_t dk_thread_lock;

extern struct dde_kit_timer dk_timers[DK_TIMER_COUNT];
extern struct dde_kit_thread dk_threads[DK_THREAD_COUNT];
/* interrupt.c */
extern spin_lock_t dk_irq_attach_lock;
extern struct dk_irq_hook_list *dk_irq_hooks[32];
extern struct dk_irq_hook_list *dk_irq_hooks_attaching[32];

void
dde_kit_init(void)
{
	 /* magic! */
	 inv_map = (dde_kit_addr_t *)malloc(1 << 22);

	 spl_init(&dk_timer_lock);
	 spl_init(&dk_thread_lock);
	 spl_init(&dk_irq_attach_lock);
	 
	 int i;
	 for (i = 0; i != DK_TIMER_COUNT; ++ i)
	 {
		  dk_timers[i].free = 1;
	 }

	 for (i = 0; i != DK_THREAD_COUNT; ++ i)
	 {
		  dk_threads[i].free = 1;
	 }

	 for (i = 0; i != 32; ++ i)
	 {
		  dk_irq_hooks[i] = NULL;
		  dk_irq_hooks_attaching[i] = NULL;
	 }

	 dde_kit_thread_adopt_myself("ekos_dlinit");

	 printf("DDE_KIT_INIT finsihed!\n");
}

void dde_kit_intr_symbol(void);
void dde_kit_lock_symbol(void);
void dde_kit_memory_symbol(void);
void dde_kit_panic_symbol(void);
void dde_kit_pci_symbol(void);
void dde_kit_pgtab_symbol(void);
void dde_kit_printf_symbol(void);
void dde_kit_resources_symbol(void);
void dde_kit_sem_symbol(void);
void dde_kit_thread_symbol(void);
void dde_kit_timer_symbol(void);

#include <eku/symbol.h>

static int
module_load(void)
{
	 dde_kit_intr_symbol();
	 dde_kit_lock_symbol();
	 dde_kit_memory_symbol();
	 dde_kit_panic_symbol();
	 dde_kit_pci_symbol();
	 dde_kit_pgtab_symbol();
	 dde_kit_printf_symbol();
	 dde_kit_resources_symbol();
	 dde_kit_sem_symbol();
	 dde_kit_thread_symbol();
	 dde_kit_timer_symbol();

	 export_symbol("dde_kit_init", &dde_kit_init, 1);

	 return 0;
}

static int
module_unload(void)
{
	 return 0;
}
